/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gate.learning.standalone;

import gate.AnnotationSet;
import gate.Corpus;
import gate.Document;
import gate.Factory;
import gate.FeatureMap;
import gate.Gate;
import gate.ProcessingResource;
import gate.creole.ResourceInstantiationException;
import gate.creole.SerialAnalyserController;
import gate.util.GateException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author asatrya
 */
public class PRExecute {
    public static void main(String[] args){
        try {
            // Gate initialization
            Gate.init();

            // Load the ANNIE plugin
            Gate.getCreoleRegister().registerDirectories(new File(Gate.getPluginsHome(), "ANNIE").toURI().toURL());

            // Create a serial analyser controller to run ANNIE with
            SerialAnalyserController annieController = (SerialAnalyserController) Factory.createResource(
                    "gate.creole.SerialAnalyserController");

            // Create and load PR: Tokenizer
            FeatureMap tokenizerParams = Factory.newFeatureMap();
            tokenizerParams.put("transducerGrammarURL", "file:/D:/APPS/gate-6.0/plugins/ANNIE/resources/tokeniser/postprocess_id.jape");
            ProcessingResource tokenizer = (ProcessingResource) Factory.createResource("gate.creole.tokeniser.DefaultTokeniser",
                    tokenizerParams);
            annieController.add(tokenizer);

            // Create and load PR: Sentence Splitter
            ProcessingResource sentenceSplitter = (ProcessingResource) Factory.createResource("gate.creole.splitter.SentenceSplitter");
            annieController.add(sentenceSplitter);

            // Print PRs name
            Iterator prIter = annieController.getPRs().iterator();
            System.out.println("Loaded PRs:");
            while(prIter.hasNext()){
                ProcessingResource currentPR = (ProcessingResource) prIter.next();
                System.out.println(currentPR.toString() + " (" + currentPR.getName() + ")");
            }

            // Load corpus
            File file = new File("D:\\PROJECT\\Riset\\Virtual SME\\Information Extraction\\Dataset\\XML GATE\\Basic10");
            URL fileUrl = file.toURL();
            Corpus corpus = Factory.newCorpus("myCorpus");
            corpus.populate(fileUrl, null, "UTF-8", true);
            annieController.setCorpus(corpus);

            // Run ANNIE
            annieController.execute();

        } catch (MalformedURLException ex) {
            Logger.getLogger(PRExecute.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PRExecute.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ResourceInstantiationException ex) {
            Logger.getLogger(PRExecute.class.getName()).log(Level.SEVERE, null, ex);
        } catch (GateException ex) {
            Logger.getLogger(PRExecute.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
